package org.chen.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class FreeMarkerUtils {

	/**
	 * @author MrChen
	 * @param templateDirectoryPath
	 *            模板文件路径
	 * @param templateFileName
	 *            模板文件名称 test.ftl
	 * @param outDirectoryPath
	 *            输出的目录
	 * @param fileName
	 *            输出的文件名
	 */
	public static void generateFile(String templateDirectoryPath, String templateFileName, Map<Object, Object> data,
			String outDirectoryPath, String fileName) {
		try {
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
			configuration.setDefaultEncoding("UTF-8");
			configuration.setDirectoryForTemplateLoading(new File(templateDirectoryPath));
			configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
			Template template = configuration.getTemplate(templateFileName);
			Writer out = new FileWriter(new File(outDirectoryPath + "/" + fileName), true);
			template.process(data, out);
			System.out.println("success");
		} catch (Exception e) {
			System.out.println("fail");
		}

	}

}
